package com.imut.lagain.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imut.lagain.entity.MoodStealRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 心情偷取记录数据访问层
 */
@Mapper
public interface MoodStealRecordRepository extends BaseMapper<MoodStealRecord> {
    
    /**
     * 根据偷取者ID查询心情偷取记录
     * @param thiefUserId 偷取者ID
     * @param page 分页参数
     * @return 心情偷取记录分页列表
     */
    @Select("SELECT * FROM mood_steal_record WHERE thief_user_id = #{thiefUserId} ORDER BY steal_time DESC")
    IPage<MoodStealRecord> findByThiefUserId(@Param("thiefUserId") Long thiefUserId, Page<MoodStealRecord> page);
    
    /**
     * 根据被偷者ID查询心情偷取记录
     * @param victimUserId 被偷者ID
     * @param page 分页参数
     * @return 心情偷取记录分页列表
     */
    @Select("SELECT * FROM mood_steal_record WHERE victim_user_id = #{victimUserId} ORDER BY steal_time DESC")
    IPage<MoodStealRecord> findByVictimUserId(@Param("victimUserId") Long victimUserId, Page<MoodStealRecord> page);
    
    /**
     * 根据小偷ID查询心情偷取记录
     * @param thiefId 小偷ID
     * @param page 分页参数
     * @return 心情偷取记录分页列表
     */
    @Select("SELECT * FROM mood_steal_record WHERE thief_id = #{thiefId} ORDER BY steal_time DESC")
    IPage<MoodStealRecord> findByThiefId(@Param("thiefId") Long thiefId, Page<MoodStealRecord> page);
    
    /**
     * 根据条目ID查询心情偷取记录
     * @param entryId 条目ID
     * @return 心情偷取记录列表
     */
    @Select("SELECT * FROM mood_steal_record WHERE entry_id = #{entryId} ORDER BY steal_time DESC")
    List<MoodStealRecord> findByEntryId(@Param("entryId") Long entryId);
    
    /**
     * 查询指定时间范围内的偷取记录
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param page 分页参数
     * @return 心情偷取记录分页列表
     */
    @Select("SELECT * FROM mood_steal_record WHERE steal_time BETWEEN #{startTime} AND #{endTime} ORDER BY steal_time DESC")
    IPage<MoodStealRecord> findByTimeRange(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime, Page<MoodStealRecord> page);
    
    /**
     * 统计偷取者ID的偷取记录数量
     * @param thiefUserId 偷取者ID
     * @return 偷取记录数量
     */
    @Select("SELECT COUNT(*) FROM mood_steal_record WHERE thief_user_id = #{thiefUserId}")
    Long countByThiefUserId(@Param("thiefUserId") Long thiefUserId);
    
    /**
     * 统计被偷者ID的被偷记录数量
     * @param victimUserId 被偷者ID
     * @return 被偷记录数量
     */
    @Select("SELECT COUNT(*) FROM mood_steal_record WHERE victim_user_id = #{victimUserId}")
    Long countByVictimUserId(@Param("victimUserId") Long victimUserId);
}
